package com.company.pushTools;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.company.core.result.Result;
import com.company.core.result.ResultUtil;
import com.company.utils.AssertUtils;
import com.company.utils.HttpClientUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Map;

@Component
public class PushBase {
    private static Logger logger = LoggerFactory.getLogger(PushBase.class);
    //推送接口
    private static final String sendMeg_path = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";

    /**
     * 获取object
     * @param objStr
     * @return
     */
    public static JSONObject getColorObj(String objStr,String color){
        JSONObject object = new JSONObject();
        object.put("value",objStr);
        object.put("color",color);
        return object;
    }


    /**
     * 配置基本参数
     * @return
     */
    public boolean initPush(PushParams pushParams) {
        JSONObject Params = new JSONObject();
        AssertUtils.isNull(pushParams.getTouser(),"touser不能为空！");
        AssertUtils.isNull(pushParams.getTemplateId(),"template_id不能为空！");
        AssertUtils.isNull(pushParams.getKeywords(),"keywords不能为空！");
        Params.put("touser",pushParams.getTouser());
        Params.put("template_id",pushParams.getTemplateId());
        if (StringUtils.isNotEmpty(pushParams.getUrl())){
            Params.put("url",pushParams.getUrl());
        }
        if (StringUtils.isNotEmpty(pushParams.getXcxPath()) && StringUtils.isNotEmpty(pushParams.getXcxAppid())){
            JSONObject xccParams = new JSONObject();
            xccParams.put("appid",pushParams.getXcxAppid());
            xccParams.put("pagepath",pushParams.getXcxPath());
            Params.put("miniprogram",xccParams);
        }


        /**
         * 下面判断是为了区分颜色的
         */
        JSONObject dataParams = new JSONObject();
        dataParams.put("first",getColorObj(pushParams.getFirst(),pushParams.getFirstColor()));
        dataParams.put("remark",getColorObj(pushParams.getRemark(),pushParams.getRemarkColor()));
        if (!CollectionUtils.isEmpty(pushParams.getKeywords())){
            //获取参数数组
            for (int i = 0;i < pushParams.getKeywords().size(); i++){
                Map<String,String> keyWordMap = pushParams.getKeywords().get(i);
                dataParams.put("keyword"+(i+1),getColorObj(keyWordMap.get("keyword"),keyWordMap.get("color")));
            }
        }
        Params.put("data",dataParams);
        return toPush(Params,sendMeg_path,pushParams.getAccessToken(),pushParams.getCount());
    }


    /**
     * 开始推送
     * @param
     * @return
     */
    public boolean toPush(JSONObject Params, String sendMeg_path, String accessToken,Integer count){
        AssertUtils.isNull(accessToken,"推送消息获取Token失败！");
        logger.info("最后的参数信息 == "+JSON.toJSONString(Params));
        JSONObject resultData = HttpClientUtils.doPostJson(sendMeg_path+accessToken, Params);
        logger.info("推送消息OK="+resultData);
        if ((int)resultData.get("errcode") == 40037){
            AssertUtils.isFalse(false,"模板ID不存在，请检查！");
        }
        if ((int)resultData.get("errcode") == 40003 && count == 1){
            AssertUtils.isFalse(false,"推送给自己验证失败，请检查！");
        }
        return true;
    }
}
